package com.shunde.xunjian.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.gson.Gson;
import com.shunde.xunjian.entity.PsLine;
import com.shunde.xunjian.entity.PsPoint;
import com.shunde.xunjian.entity.ReportOrders;
import com.shunde.xunjian.mapper.dbnetworkshunde.PsLineMapper;
import com.shunde.xunjian.mapper.dbnetworkshunde.PsPointMapper;
import com.shunde.xunjian.mapper.dbnetworkshunde.ReportOrdersMapper;
import com.shunde.xunjian.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class XunJianServiceImpl {
    @Autowired
    private PsPointMapper psPointMapper;

    @Autowired
    private PsLineMapper psLineMapper;

    @Autowired
    private ReportOrdersMapper reportOrdersMapper;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");

    //空间查询设施附近工单
    public Object WorkOrderInfosByGeo(String wkt) {
        QueryWrapper<PsPoint> psPointQueryWrapper = new QueryWrapper<>();
        QueryWrapper<PsLine> psLineQueryWrapper = new QueryWrapper<>();
        if (wkt != null) {
            System.out.println(wkt);
            psPointQueryWrapper.apply("ST_DWithin(shape, ST_GeomFromText({0}),5)", wkt);
            psLineQueryWrapper.apply("ST_DWithin(shape, ST_GeomFromText({0}),5)", wkt);
        }
        List<PsPoint> psPoints = psPointMapper.selectList(psPointQueryWrapper.orderByAsc("ST_Distance(shape, ST_GeomFromText('" + wkt + "'))"));
        List<PsLine> psLines = psLineMapper.selectList(psLineQueryWrapper.orderByAsc("ST_Distance(shape, ST_GeomFromText('" + wkt + "'))"));
        Map<Object, Object> map = new HashMap<>();
        map.put("psPoints", psPoints);
        map.put("psLines", psLines);
        return map;
    }

    //设施编号查询对应坐标
    public Object facilityInfo(String facilityId) {
        QueryWrapper<PsPoint> psPointQueryWrapper = new QueryWrapper<>();
        QueryWrapper<PsLine> psLineQueryWrapper = new QueryWrapper<>();
        PsPoint psPoint = psPointMapper.selectOne(psPointQueryWrapper.eq("survey_no", facilityId));
        PsLine psLine = psLineMapper.selectOne(psLineQueryWrapper.eq("concat(fromsurveyno, '-',tosurveyno)", facilityId));
        Map<Object, Object> map = new HashMap<>();
        map.put("psPoint", psPoint);
        map.put("psLine", psLine);
        return map;
    }

}
